﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Teradata.Client.Provider;
using System.Configuration;
using System.Text;
using System.Data;
using Teradata.Client.Provider;

public partial class _getChartData2 : System.Web.UI.Page
{
    protected String ViewName = String.Empty;
    protected String ColumnName = String.Empty;
    protected String[] Months = { "Joe", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
    protected String ConnString = ConfigurationManager.ConnectionStrings["EDW_GROUP_ID"].ConnectionString;



    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime startTime = DateTime.Now; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Started");
        Response.ContentType = "text/xml";
        Response.Write(getHeader() + getChartData(Request.QueryString["SQLFILTER"], Request.QueryString["LEGEND"]) + getFooter());

        DateTime stopTime = DateTime.Now;
        TimeSpan duration = stopTime - startTime;
        Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Ended: " + duration.ToString());
    }

    protected string getHeader()
    {
        RollupMetadata meta = (RollupMetadata)Session["RollupMetadata"];
        string sHeight = (meta.DisplayMonthly() ? "480" : "300");
        string sWidth = (meta.DisplayMonthly() ? "800" : "500");

        string sTemplate = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("templates\\Chart2Header.xaml"));
        return String.Format(sTemplate, sWidth, sHeight, meta.MonthlyLabel.Replace("&","&amp;"));

        //"  <vc:DataSeries RenderAs=\"Column\" AxisYType=\"Primary\" Bevel=\"True\" LabelEnabled=\"True\"  YValueFormatString=\"  ###,###\"> " +
        //"   <vc:DataSeries.DataPoints> ";
    }

    protected string getFooter()
    {
        return " </vc:Chart.Series> " +
               "</vc:Chart>";

    }


    protected string getChartData(String SQLFILTER, string psLegendText)
    {
        DateTime startTime = DateTime.Now; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Started");

        StringBuilder sb = new StringBuilder();

        string ROLLUPTYPE = SQLFILTER.Split('=')[0].Trim();

        if (ROLLUPTYPE != null)
        {
            RollupMetadata meta = (RollupMetadata)Session["RollupMetadata"];
            DataView dv = (new RolledUpBiz(ROLLUPTYPE)).MonthlyChartData(SQLFILTER, true);//meta.ChkStacked.Checked
            bool bStacked = dv.Count > 13;
            for (int j = 0; j < dv.Count / 13; j++) //j is the set number
            {
                //string sLegend = (dv[0].Row["LOCATION"] + " " + dv[0].Row["DWELLING"] + " " + dv[0].Row["PRODUCT"]).Trim();
                string sLegendText = "";
                //string sLegendText = bStacked ? " LegendText=\"" + sLegend + "\" " : "";
                try { sLegendText = bStacked ? " LegendText=\"" + psLegendText.Split('*')[j] + "\" " : ""; }
                catch { }

                string sFormat = meta.DisplayMonthly()?"   #,###":"#";
                sb.Append(
                    string.Format(
                "  <vc:DataSeries RenderAs=\"{0}Column\" AxisYType=\"Primary\" Bevel=\"True\" LabelEnabled=\"True\"" +
                "  YValueFormatString=\"{2}\"  {1}  > " +
                "   <vc:DataSeries.DataPoints> ", bStacked ? "Stacked" : "", sLegendText, sFormat)
                );

                for (int k = 0; k < 13; k++)
                {
                    int i = k + 13 * j;
                    sb.Append(String.Format("    <vc:DataPoint AxisXLabel=\"{1} '{0}\" YValue=\"{2}\"  Cursor=\"Hand\" />",
                       dv[i].Row["YR"].ToString().Substring(2),
                       Months[(int)dv[i].Row["MN"]],
                       (int)dv[i].Row["TOT_NET"]
                       ));
                }
                sb.Append("   </vc:DataSeries.DataPoints>  </vc:DataSeries> ");

            }
        }

        return sb.ToString();
    }


}



